//
//  ViewController.m
//  MyShiDiQuan
//
//  Created by cvicseks-mac1 on 16/7/27.
//  Copyright © 2016年 cvicseks-mac1. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "Networkenable.h"
#import "AFHTTPSessionManager.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    UINavigationBar *bar = [[UINavigationBar alloc] init];
    [self.view addSubview:bar];
    bar.barStyle = UIBarStyleBlackTranslucent;
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(rectStatus.size.height);
        make.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    [Networkenable isNetworkEnable];
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    UIButton * genxin = [[UIButton alloc] init];
    [genxin setTitle:@"更新" forState:UIControlStateNormal];
    [genxin setBackgroundColor:[UIColor redColor]];
    [genxin addTarget:self action: @selector(genxin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:genxin];
    [genxin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(50);
        make.top.equalTo(self.view).with.offset(rectStatus.size.height+100);
        make.right.equalTo(self.view).with.offset(-50);
        make.height.equalTo(@30);
    }];
}

- (void)genxin{
    //得到CFBundleIdentifier
    NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
     NSLog(@"bundleId为：%@",bundleId);
    //设置请求地址
    NSString *requestURLStr = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@",bundleId];
    //解决UTF-8乱码问题
    requestURLStr = [requestURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setStringEncoding:NSUTF8StringEncoding];
    //Judy-mark:声明请求与返回的数据类型，二者缺一不可!
    //    manager.requestSerializer = [AFJSONRequestSerializer serializer];//设置JSON内容类型的编码请求
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//设置JSON内容类型的编码请求
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

    [manager POST:requestURLStr parameters:@"hi" success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        int resultCount = [[responseObject objectForKey:@"resultCount"]intValue];
        if(resultCount != 0){
            NSArray *array = [responseObject objectForKey:@"results"];
            NSDictionary *dic = [array objectAtIndex:0];
            NSString *version = [dic objectForKey:@"version"];//得到服务器的版本
            NSString *trackViewUrl = [dic objectForKey:@"trackViewUrl"];//得到服务器的地址
            
//            version = @"1.0";//得到服务器的版本
            
            NSLog(@"appstore服务器的版本为：%@",version);
            NSString *lVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
            NSLog(@"本地版本：%@",lVersion);
            
            if(![lVersion isEqualToString:version]){
                //向name发送通知，object就是要传的值。UserInfo是一个字典,如果要用的话,提前定义一个字典,可以通过这个来实现多个参数的传值使用。
//                [[NSNotificationCenter defaultCenter]postNotificationName:@"versionUpdate" object:trackViewUrl];
                NSLog(@"版本不一致");

            }
        }else{
            NSLog(@"并没有在APP Store上找到该应用");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"版本更新失败。。。");

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
